package LeetCode;

public class LC_424_LongestRepeatingCharacterReplacement {

    public static void main(String[] args) {

    }

    static class Solution {
        public int characterReplacement(String s, int k) {
            if (s.length() < 2) return s.length();
            int r = 0, l = 0, maxCount = 0, maxLength = 0;
            int[] letters = new int[26];
            while (r < s.length()) {
                int letter = s.charAt(r) - 'A';
                maxCount = Math.max(maxCount, ++letters[letter]);
                if (r - l + 1 > maxCount + k) {
                    letters[s.charAt(l) - 'A']--;
                    l++;
                }
                maxLength = Math.max(maxLength, r - l + 1);
                r++;
            }
            return maxLength;
        }
    }

}